<?php
	require('config.php');
	$msg = array();
	$postStr = file_get_contents('php://input');
	$msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
	file_put_contents('wechatchongzhihuidiao.txt',json_encode($msg).PHP_EOL,FILE_APPEND);
	if($msg['result_code']=='SUCCESS')
	{
		$user_id=$msg['attach'];
        $money=$msg['total_fee']/100;
        $type='weixin';
        $orderNo='CZ'.time();
        $time=date('Y-m-d H:i:s');
        $time2=time();
		$sql="select * from `".$oto."_users` where `userId`='".$user_id."'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$user_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$sql="select * from `".$oto."_money_record` where `orderNo`='".$orderNo."'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$record[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(!isset($record))
		{
			$balance=$user_info[0]['userMoney']+$money;
			if($type=='weixin')
			{
				$cz_type='微信充值';
				$pay_way_lin=2;
			} 
			if($type=='alipay')
			{
				$cz_type='支付宝充值';
				$pay_way_lin=1;
			} 
			$hmoney=$user_info[0]['userMoney']+$money;
			$sql="update ".$oto."_users set userMoney='$hmoney' where userId='{$user_id}'";
			$result2=$db->query($sql);

			$third_trade_no = (string)$msg['out_trade_no'];
			$sql="INSERT INTO ".$oto."_money_record (`userid`,`orderNo`,`money`,`balance`,`time`,`IncDec`,`type`,`remark`,`payWay`,`third_trade_no`) VALUES ('$user_id','$orderNo','$money','$balance','$time2','1','3','$cz_type','$pay_way_lin','$third_trade_no')";
            $result=$db->query($sql);
            if($result2)
            {
            	$data[0]['pan']='true';
            	echo "SUCCESS";
            }
            else
            {
            	$data[0]['pan']='false';
            }
		}
		//echo "SUCCESS";
		//return true;
		//file_put_contents("tsxx_WxPay.txt", "\r\n------------订单支付成功--------------\r\n", FILE_APPEND);
		// response.getWriter().println("<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");
		 // 告诉微信服务器，我收到信息了，不要在调用回调action了
	}
?>